package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class Bookcontroller {
    @Autowired
    private BookService bookService;


    @GetMapping("getlist3")
    @ResponseBody
    public MyResult getlist(BookVO bookVO) {
        if (bookVO.getPage() == 0) {
            bookVO.setPage(1);
        }
        int b = (bookVO.getPage() - 1) * bookVO.getLimit();
        bookVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Book> getpage = bookService.getpage3(bookVO);
        Integer getcount = bookService.getcount3(bookVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,getpage);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;
    }

    @GetMapping("/delete3")
    @ResponseBody
    public Integer delete(Integer id) {
        Integer integer = bookService.delete3(id);
        return integer;
    }

    //内容详情
    @PostMapping("/tanchuid3/{id}")
    @ResponseBody
    public Map<String, Object> tanchu(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        Book tanchu = bookService.tanchu3(id);
        map.put("code", 0);
        map.put("msg", "注册成功！");
        map.put("count", 1);
        map.put("data", tanchu);
        return map;

    }

    @PostMapping("/update3")
    @ResponseBody
    public Map gengxin(Book book){
        Map map = new HashMap();
        int update = bookService.update3(book);
        map.put("code", 0);
        map.put("msg", "更新成功！");
        map.put("count", 1);
        map.put("data", update);
        return map;
    }

    @PostMapping("/insert3")
    @ResponseBody
    public Map<String,Object> insert(Book book){
        Map<String, Object> map = new HashMap<>();
        Integer integer = bookService.insert3(book);
        map.put("code", 0);
        map.put("msg", "请求成功！");
        map.put("count", 1);
        map.put("data", integer);
        return map;
    }

    @GetMapping("/Bydelete3")
    @ResponseBody
    public Map<String,Object> Bydelete(Integer[] ids){
        Map<String, Object> map = new HashMap<>();
        Integer re = null;
//        for (Integer i: ids) {
        re = bookService.Bydelete3(ids);
//        }
        map.put("code", 0);
        map.put("msg", "请求成功！");
        map.put("count", 1);
        map.put("data", re);
        return map;
    }


    @GetMapping("getlist9")
    @ResponseBody
    public MyResult getlist1(BookVO bookVO) {
        if (bookVO.getPage() == 0) {
            bookVO.setPage(1);
        }
        int b = (bookVO.getPage() - 1) * bookVO.getLimit();
        bookVO.setB(b);
        Map<String, Object> map = new HashMap<>();
        List<Book> getpage = bookService.getpage9(bookVO);
        Integer getcount = bookService.getcount9(bookVO);

        MyResult result = new MyResult(0,"注册成功！",getcount,getpage);
//        map.put("code", 0);
//        map.put("msg", "注册成功！");
//        map.put("count", getcount);
//        map.put("data", getpage);
        return result;
    }
}
